package com.ittest.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ittest.domain.system.Permission;
import com.ittest.domain.system.Role;
import com.ittest.domain.system.SysLog;
import com.ittest.factory.MapperFactory;
import com.ittest.mapper.system.PermissionMapper;
import com.ittest.mapper.system.RoleMapper;
import com.ittest.mapper.system.SysLogMapper;
import com.ittest.service.system.RoleService;
import com.ittest.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 角色管理实现类
 */
public class RoleServiceImpl implements RoleService {
    /**
     * 模糊查询
     * @param page
     * @param size
     * @param nameStr
     * @return
     */
    @Override
    public PageInfo findByName(int page, int size, String nameStr) {
        // 获取SqlSession
        SqlSession sqlSession;
        PageInfo pageInfo = null;
        try{
            // 1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            // 2. 获取Mapper
            RoleMapper mapper = MapperFactory.getMapper(sqlSession, RoleMapper.class);
            // 3.调用Dao层操作
            PageHelper.startPage(page, size);
            // nameStr非空处理
            if (StringUtils.isNotBlank(nameStr)){
                nameStr = nameStr.replaceAll(" ","");
            }
            // 调用Mapper层
            List<SysLog> all = mapper.findByName(nameStr);

            // 4.分页 获取参数，设置当前页面和每页数据条数
            pageInfo = new PageInfo(all);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * 添加操作
     * @param role
     */
    @Override
    public void add(Role role) {
        // 声明对象
        SqlSession session= null;
        try {
            session = MapperFactory.getSqlSession();
            //获取mapper对象
            RoleMapper mapper= MapperFactory.getMapper(session,RoleMapper.class);
            String id = UUID.randomUUID().toString();
            role.setId(id);
            mapper.save(role);
            //提交
            TransactionUtil.commit(session);
        } catch (Exception e) {
            // 失败回滚
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(session);
        }

    }


    /**
     * 查询所有
     * @return List<Role> 角色列表
     */
    @Override
    public List<Role> findAll() {
        // 声明对象
        SqlSession sqlSession = null;
        List<Role> list = null;
        try {
            // 1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            // 2. 获取Mapper
            RoleMapper mapper = MapperFactory.getMapper(sqlSession, RoleMapper.class);
            // 3.调用Dao层操作
            list = mapper.findAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteByIds(String arr) {
        // 声明SqlSession对象
        SqlSession sqlSession = null;
        try {
            // 1. 获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            // 2. 获取Dao
            RoleMapper mapper = MapperFactory.getMapper(sqlSession, RoleMapper.class);
            // 3. 调用Dao层操作
            // 分割字符串
            if (arr != null && arr.length() > 0) {
                String[] ids = arr.split(",");
                mapper.deleteByIds(ids);
            }
            // 4. 提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            // 出异常，回滚事务
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                // 释放资源
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public List<Permission> findPermissionByRole(String id) {
        SqlSession sqlSession = null;
        List<Permission> list = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper对象
            PermissionMapper mapper = MapperFactory.getMapper(sqlSession, PermissionMapper.class);
            // 调用方法
            list = mapper.findPermissionByRole(id);
        } catch (Exception e) {
            throw  new RuntimeException(e);
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return list;
    }
    @Override
    public void addPermissionToRole(String roleId, String[] permissionIds) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取对应的mapper对象
            RoleMapper mapper = MapperFactory.getMapper(sqlSession, RoleMapper.class);
            // 先删除所有的角色
            mapper.deletePermission(roleId);
            for (String permissionId : permissionIds) {
                mapper.updatePermission(roleId,permissionId);
            }

            // 提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            // 记录日志
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }

}
